package com.gtw.modules.api.controller;

import com.gtw.common.annotation.LoginUserApi;
import com.gtw.common.utils.R;
import com.gtw.modules.api.service.TbUserService;
import com.gtw.modules.entity.TbDiscount;
import com.gtw.modules.entity.TbDiscuss;
import com.gtw.modules.entity.TbProxyFavorable;
import com.gtw.modules.entity.TbUser;
import com.gtw.modules.sys.service.SysDiscountService;
import com.gtw.modules.sys.service.TbProxyFavorableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 优惠
 */
@RestController
@RequestMapping("/api/discount")
public class ApiDiscountController {

    @Autowired
    private TbProxyFavorableService tbProxyFavorableService;

    @Autowired
    private TbUserService tbUserService;


    /**
     * 根据金额判断用户应加库存
     *
     * @param money
     * @param tbUser
     * @return
     */
    @RequestMapping("/findByMoney")
    public R findByMoney(int money, @LoginUserApi TbUser tbUser) {
        List<TbProxyFavorable> tbProxyFavorableList = tbProxyFavorableService.queryAll();
        for (int i = 0; i < tbProxyFavorableList.size(); i++) {
            if (money > tbProxyFavorableList.get(i).getMinprice() && money < tbProxyFavorableList.get(i).getMaxprice()) {
                if (tbUser.getType() == 2) {
                    tbUser.setAgentinventory(money);
                    tbUserService.update(tbUser);
                }
            }
        }
        return R.ok();
    }

}
